package net.toocruel.iqismart.entity.common;

import net.toocruel.iqismart.entity.base.BaseEntity;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Auther sty
 * @createTime 2018/5/6 上午4:20
 */
@Entity
public class Area  extends BaseEntity {
    /**
     * 父
     */
    @ManyToOne(cascade = { CascadeType.MERGE}, optional=true)
    @JoinColumn(name = "parentId")
    private Area parent;
    /**
     * 子
     */
    @OneToMany(cascade = { CascadeType.ALL}, fetch=FetchType.EAGER, mappedBy="parent")
    private List<Area> childs = new ArrayList<>();

    private String name;
    private int grade;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    public Area getParent() {
        return parent;
    }

    public void setParent(Area parent) {
        this.parent = parent;
    }

    public List<Area> getChilds() {
        return childs;
    }

    public void setChilds(List<Area> childs) {
        this.childs = childs;
    }

    @Override
    public String toString() {
        return name==null?"":name;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Area area = (Area) o;
        return grade == area.grade &&
                Objects.equals(name, area.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, grade);
    }
}
